import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/NewLogin',
    name: 'NewLogin',
    meta: {
      keepAlive: true,
      title: "用户登录"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/back/NewLogin')
  },
  // {
  //   path: '/login',
  //   name: 'Login',
  //   meta: {
  //     keepAlive: true,
  //     title: "用户登录"
  //   },
  //   component: () =>
  //     import ( /* webpackChunkName: "about" */ '../views/back/Login')
  // },
  {
    path: '/register',
    name: 'Register',
    meta: {
      title: "用户注册"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/back/Register')
  },
  {
    path: '/requestInvalid',
    name: 'RequestInvalid',
    meta: {
      title: "404"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/back/RequestInvalid')
  },
  {
    path: '/front',
    name: 'Front',
    meta: {
      title: "前台商城"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/front/Front'),
    children: [{
        path: 'home',
        name: 'Home',
        meta: {
          title: "前台商城"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Home'),
      },
      {
        path: 'index',
        name: 'index',
        meta: {
          title: "代码在线编译器"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/monaco-editor/index'),
      },
      {
        path: 'user',
        name: 'User',
        meta: {
          title: "个人中心"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/User/User.vue'),
      },
      {
        path: '/Details/details',
        name: 'Details',
        meta: {
          title: "商品详情"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Details/Details'),
      },
      {
        path: '/Details/kecheng-details',
        name: 'Kecheng-Details',
        meta: {
          title: "课程详情"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Details/Kecheng-Details'),

      },
      {
        path: 'video',
        name: 'Video',
        meta: {
          title: "视频展示"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Video')
      },
      {
        path: 'videoDetail',
        name: 'VideoDetail',
        meta: {
          title: "视频播放"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/VideoDetail')
      },
      {
        path: 'chonzhi',
        name: 'Chonzhi',
        meta: {
          title: "充值"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Chonzhi'),
      },
      {
        path: 'kecheng',
        name: 'Kecheng',
        meta: {
          title: "课程"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Kecheng'),
      },
      {
        path: 'shouye',
        name: 'Shouye',
        meta: {
          title: "首页"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Shouye'),
      },
      {
        path: 'live',
        name: 'live',
        meta: {
          title: "直播"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/live/live'),
      },
      {
        path: 'vip',
        name: 'Vip',
        meta: {
          title: "会员购"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Vip'),
      },
      {
        path: 'biji',
        name: 'biji',
        meta: {
          title: "笔记"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/collect/biji'),
      },
      {
        path: 'xinxi',
        name: 'Xinxi',
        meta: {
          title: "消息首页"
        },
        redirect: '/front/xinxi/huifu',
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Xinxi'),
        children: [{
            path: 'huifu',
            name: 'Huifu',
            meta: {
              title: "回复我的"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/huifu'),
          },
          {
            path: 'wode',
            name: 'Wode',
            meta: {
              title: "@我的"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/wode'),
          },
          {
            path: 'shoudao',
            name: 'Shoudao',
            meta: {
              title: "收到的赞"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/shoudao'),
          },
          {
            path: 'xitong',
            name: 'Xitong',
            meta: {
              title: "系统通知"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/xitong'),
          },
          {
            path: 'cundang',
            name: 'Cundang',
            meta: {
              title: "存档"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/cundang'),
          },
          {
            path: 'wdxiaoxi',
            name: 'Wdxiaoxi',
            meta: {
              title: "我的消息"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/xiaoxi/wdxiaoxi'),
          },
        ]
      },
      {
        path: 'dontai',
        name: 'Dontai',
        meta: {
          title: "动态"
        },
        redirect: '/front/Dontai/whole',
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Dontai'),
        children: [{
            path: 'code',
            name: 'code',
            meta: {
              title: "代码"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/Dongtai/code'),
          },
          {
            path: 'minaAticle',
            name: 'minaAticle',
            meta: {
              title: "文章"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/Dongtai/minaAticle'),
          },
          {
            path: 'video-view',
            name: 'video-view',
            meta: {
              title: "视频"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/Dongtai/video-view'),
          },
          {
            path: 'whole',
            name: 'whole',
            meta: {
              title: "全部"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/Dongtai/whole'),
          },
          {
            path: 'naturalResources',
            name: 'naturalResources',
            meta: {
              title: "资源"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/Dongtai/naturalResources'),
          },
        ]
      },
      {
        path: 'shoucang',
        name: 'Shoucang',
        meta: {
          title: "收藏"
        },
        redirect: '/front/shoucang/huati',
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Shoucang'),
        children: [{
            path: 'huati',
            name: 'huati',
            meta: {
              title: "话题收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/huati'),
          },
          {
            path: 'zhuanti',
            name: 'zhuanti',
            meta: {
              title: "专题收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/zhuanti'),
          },
          {
            path: 'zhuanlan',
            name: 'zhuanlan',
            meta: {
              title: "专栏收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/zhuanlan'),
          },
          {
            path: 'xiangce',
            name: 'xiangce',
            meta: {
              title: "相册收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/xiangce'),
          },
          {
            path: 'minKecheng',
            name: 'minKecheng',
            meta: {
              title: "课程收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/min-kecheng'),
          },
          {
            path: 'moren',
            name: 'moren',
            meta: {
              title: "默认收藏夹"
            },
            component: () =>
              import ( /* webpackChunkName: "about" */ '../views/front/collect/moren'),
          },
        ]
      },
      {
        path: 'changePassword',
        name: 'FrontChangePassword',
        meta: {
          title: "修改密码"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/ChangePassword')
      },
      {
        path: 'person',
        name: 'FrontPersson',
        meta: {
          title: "个人中心"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Person')
      },
      {
        path: 'article',
        name: 'Article',
        meta: {
          title: "文章中心"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/Article')
      },
      {
        path: 'articleDetail',
        name: 'ArticleDetail',
        meta: {
          title: "文章详情"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/ArticleDetail')
      },
      {
        path: 'shoppingTrolley',
        name: 'ShoppingTrolley',
        meta: {
          title: "购物车"
        },
        component: () =>
          import ( /* webpackChunkName: "about" */ '../views/front/ShoppingTrolley')
      }
    ]
  },
  //博客页面
  {
    path: '/blogHome',
    name: 'BlogHome',
    meta: {
      title: "社区主页"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/pages/Home')
  },
  {
    path: '/NewBK',
    name: 'NewBK',
    meta: {
      title: "小陈开发"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/NewViews/NewBK')
  },
  {
    path: '/detailShare',
    name: 'DetailShare',
    meta: {
      title: "文章详情"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/pages/DetailArticle')
  },
  {
    path: '/userInfo',
    name: 'UserInfo',
    meta: {
      title: "个人中心"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/pages/UserInfo')
  },
  {
    path: '/blogChangePassword',
    name: 'BlogChangePassword',
    meta: {
      title: "修改密码"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/pages/ChangePassword')
  },
  //过渡动画
  {
    path: '/load',
    name: 'Load',
    meta: {
      title: "加载中..."
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../common/Load')
  },
  //音乐播放界面
  {
    path: '/music/home',
    name: 'Home',
    meta: {
      title: "jay"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/views/music/Home')
  },
  //音乐播放界面
  {
    path: '/music/butMusic',
    name: 'ButMusic',
    meta: {
      title: "jay"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/views/music/ButMusic')
  },
  //聊天窗口+chatgpt+私信
  {
    path: '/chat',
    name: 'ButMusic',
    meta: {
      title: "🏂聊天私信室"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/views/chatgpt/ChatHome'),
    children: [{
        //替换 /home 字符串为 home
        path: "/",
        name: "chatDefault",
        meta: {
          title: "✋声明"
        },
        component: () =>
          import ("../views/BlogFront/views/chatgpt/Chat-default"),

      },
      {
        //替换 /home 字符串为 home
        path: "chatgpt",
        name: "chatgpt",
        meta: {
          title: "🤖ChatGPT"
        },
        component: () =>
          import ("../views/BlogFront/views/chatgpt/ChatGPT"),
        children: [{
            //替换 /home 字符串为 home
            path: "/",
            name: "chatGPT-View",
            meta: {
              title: "🤖ChatGPT"
            },
            component: () =>
              import ("../views/BlogFront/views/chatgpt/chat-views/chatGPT-View"),
          },
          {
            //替换 /home 字符串为 home
            path: "ai-painting",
            name: "aiPainting",
            meta: {
              title: "AI绘画🎨"
            },
            component: () =>
              import ("../views/BlogFront/views/chatgpt/chat-views/chatGPT-AiPainting"),
          }
        ]
      },
      {
        //替换 /home 字符串为 home
        path: "virtualPerson",
        name: "virtualPerson",
        meta: {
          title: "🤖ai虚拟数字人"
        },
        component: () =>
          import ("../views/BlogFront/views/chatgpt/Virtual-digital-person"),
      },
      {
        //替换 /home 字符串为 home
        path: "comment",
        name: "comment",
        meta: {
          title: "消息"
        },
        component: () =>
          import ("../views/BlogFront/views/chatgpt/Chat-comment"),
      },
      {
        //替换 /home 字符串为 home
        path: "bell",
        name: "bell",
        meta: {
          title: "@我的"
        },
        component: () =>
          import ("../views/BlogFront/views/chatgpt/Chat-bell"),
      }
    ]
  },
  {
    path: '/phoneLogin',
    name: 'phoneLogin',
    meta: {
      title: "移动端登录"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/phone/phoneLogin')
  },
  {
    path: '/phoneRegister',
    name: 'phoneRegister',
    meta: {
      title: "移动端注册"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/phone/phoneRegister')
  },
  {
    path: '/text',
    name: 'text',
    meta: {
      title: "语音"
    },
    component: () =>
      import ( /* webpackChunkName: "about" */ '../views/BlogFront/views/chatgpt/chat-views/Text')
  },

]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

//注意刷新页面会重置路由
export const setRouters = () => {
  const storeMenus = localStorage.getItem("user") === null ? [] : JSON.parse(localStorage.getItem("user")).menus;

  if (storeMenus) {
    // //获取当前的路由对象名称
    const currentRouterNames = router.getRoutes().map(v => v.name);
    if (!currentRouterNames.includes("Manage")) {
      //确定主路由  开始拼装动态路由
      const manageRouter = {
        path: '/',
        name: 'Manage',
        meta: {
          title: "管理系统"
        },
        component: () =>
          import ("@/views/back/Manage"),
        children: [{
            //替换 /home 字符串为 home
            path: "/person",
            name: "Person",
            meta: {
              title: "个人中心"
            },
            component: () =>
              import ("../views/back/Person"),
          },
          {
            //替换 /home 字符串为 home
            path: "/changePassword",
            name: "ChangePassword",
            meta: {
              title: "修改密码"
            },
            component: () =>
              import ("../views/back/ChangePassword"),
          }
        ]
      };
      //添加子路由
      storeMenus.forEach(item => {
        if (item.path !== "") {
          let itemMenu = {
            //替换 /home 字符串为 home
            path: item.path.slice(1),
            name: item.pagePath === null || item.pagePath === "" ? "" : item.pagePath,
            meta: {
              title: item.name
            },
            component: () =>
              import ("../views/back/" + item.pagePath),
          }
          manageRouter.children.push(itemMenu); //将子路由添加
        } else {
          item.children.forEach(itemChildren => {
            let itemMenu = {
              //替换 /home 字符串为 home
              path: itemChildren.path.slice(1),
              name: itemChildren.pagePath,
              meta: {
                title: itemChildren.name
              },
              component: () =>
                import ("../views/back/" + itemChildren.pagePath),
            }
            manageRouter.children.push(itemMenu); //将子路由添加
          })
        }

      })

      router.addRoute(manageRouter)
    }
  }
}

setRouters()

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  const storeMenus = localStorage.getItem("user") === null ? [] : JSON.parse(localStorage.getItem("user")).menus;


  document.title = to.meta.title || 'AI开放社区' //修改网页的title


  if (!to.matched.length) {
    if (storeMenus === undefined) {
      next("/requestInvalid");
    } else {
      //跳回login
      console.log("走到了")
      next("/NewLogin")
    }
  }

  if (to.fullPath === "/") {
    next("/NewBK");
  }

  next();
})


export default router
